﻿(function () {
    oo('class', {
        prop: [
            { name: 'isComplete', value: true, isStatic: true },
            { name: 'seller', isStatic: true },
            { name: 'keyword', value: '', isStatic: true },
            {
                name: 'category', value: '', setter: function (value, self) {
                    oo('ajax', {
                        url: 'getProducts.do',
                        data: {
                            seller: this.prop('seller'),
                            category: value,
                            type: this.prop('type')
                        },
                        success: function (result) {
                            self.prop('isComplete', true);
                            oo('product').init(result);
                        }
                    });
                }, isStatic: true
            },
            {
                name: 'type', value: '', setter: function (value, self) {
                    this(value);
                    self.prop('isComplete', false).getProductsAndCategoriesAndGenerateHtml();
                }, isStatic: true
            }
        ],
        $inventory: function () {
            var self = this;
            oo('category').prop('chooseCallback', function (id) {
                self.prop('isComplete') && self.prop('isComplete', false).setCategory(id, self);
            });
            oo('product').prop('addToBasketCallback', function (id, qty) {
                self.prop('isComplete') && self.prop('isComplete', false).addToBasket(id, qty, self);
            });
            $('#txtInventorySearch').combobox({
                width: 300,
                panelWidth: 300,
                panelHeight: 205,
                url: 'getKeywords.do',
                mode: 'remote',
                valueField: 'value',
                textField: 'value'
            });
            $('#btnInventorySearch').click(function () {
                self.prop('keyword', $('#txtInventorySearch').combobox('getText')).getProductsAndCategoriesAndGenerateHtml();
            });
        },
        _inventory: null,
        getProductsAndCategoriesAndGenerateHtml: function () {
            oo('ajax', {
                url: 'getProductsAndCategories.do',
                data: {
                    seller: this.prop('seller'),
                    keyword: this.prop('keyword'),
                    type: this.prop('type')
                },
                success: (function (self) {
                    return function (result) {
                        self.prop('isComplete', true);
                        oo('product').init(result.products);
                        oo('category').init(result.categories);
                    };
                })(this)
            });
        },
        setCategory: function (id, self) {
            oo('ajax', {
                url: 'getProducts.do',
                data: {
                    seller: this.prop('seller'),
                    keyword: this.prop('keyword'),
                    category: id,
                    type: this.prop('type')
                },
                success: function (result) {
                    self.prop('isComplete', true);
                    oo('product').init(result);
                }
            });
        },
        addToBasket: function (id, qty, self) {
            oo('ajax', {
                url: 'addToBasket.do',
                dataType: 'text',
                data: {
                    seller: self.prop('seller'),
                    product: id,
                    qty: qty
                },
                success: function () {
                    self.prop('isComplete', true);
                }
            });
        }
    });
})();